package com.shanmiapp;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class PrintModule extends ReactContextBaseJavaModule {

    public static final String RN_NAME = "PrintExample";
    private ReactApplicationContext mContext;

    public PrintModule(ReactApplicationContext reactContext) {
        super(reactContext);
        mContext = reactContext;
    }

    @Override
    public String getName() {
        return RN_NAME;
    }

    @ReactMethod
    public void event(String query) {
        executeResult("ar", "native--》rn", query);
    }

    @ReactMethod
    public void eventCallback(Callback Callback) {
        String msg = "我是==>>Callback回调";
        Callback.invoke(msg);
    }

    @ReactMethod
    public void eventPromise(Promise Promise) {
        String msg = "我是==>>Promise回调";
        Promise.resolve(msg);
    }

    private void executeResult(String methodName, String message, String data) {
        WritableMap writableMap = new WritableNativeMap();
        writableMap.putString("message", message);
        writableMap.putString("data", data);
        mContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(methodName, writableMap);
    }

}

